clear
set more off

// author: Lucas

cap log close
log using "$logdir/analysis/overall_success.log", replace

local incvar soi_agi
do "$adir/get_bad_dummies"

use $statadir/soi_cdw_taxsim_2019 if nonfiler == 0, clear

// calculate SOI tax liability

*****************************************
*										*
*				Tax errors				*
*										*
*****************************************
gen double delta1 = soi_fed_incm_tax - taxsim1_fed_incm_tax
gen double delta4 = soi_fed_incm_tax - taxsim4_fed_incm_tax

local tax_thrlist 10 50 100 200 500
foreach s in 1 4 {
	forval i = 1/5 {
		local thr : word `i' of `tax_thrlist'
		qui gen lb`i'_`s' = abs(delta`s') < `thr'
		qui replace lb`i'_`s' = 0 if missing(delta`s')
	}
}


*****************************************
*										*
*		Compute the bad dummies			*
*										*
*****************************************

local thrlist 			100 200 500 1000 5000
//local cumul_thrlist 	200 500 1000 5000 10000
local cred_thrlist 		10	50	100	200	500


foreach s in 1 4 {
	forval i = 1/5 {

		local thr : word `i' of `thrlist'
		//local cumul_thr : word `i' of `cumul_thrlist'
		local cred_thr : word `i' of `cred_thrlist'


		if `s' == 1 {
			local dropvars ""
		}
		if `s' == 4 {
			local dropvars bad_fil_stat bad_deps bad_eitc_deps
		}
		preserve
		get_bad_dummies, thr(`thr') cred_thr(`cred_thr') dropvars(`dropvars')
	
		if `i' == 3 & `s' == 1 {
			keep prim_tin bad_*
			gen ub`i'_`s' = 1- bad_any
			keep prim_tin bad_* ub`i'_`s'
		}
		else {
			gen ub`i'_`s' = 1- bad_any
			keep prim_tin ub`i'_`s'
		}
		tempfile d`i'_`s'
		qui save `d`i'_`s'', replace
		restore
	
	}
}

drop bad_*
forval i = 1/5 {
	foreach s in 1 4 {
		qui merge 1:1 prim_tin using `d`i'_`s'', keep(3) nogen
	}
}


*********************************************
*											*
*			Report some means				*
*											*
*********************************************

sum lb* ub* bad_* [aw=soi_wgt]

qui sum lb3_1 [iw=soi_wgt]
di r(sum)*10^-6

qui sum ub3_1 [iw=soi_wgt]
di r(sum)*10^-6

sum ub3_1 lb3_1 if cdw_nec > 0



*********************************************
*											*
*			Write simple table				*
*											*
*********************************************
cap file close myfile
qui file open myfile using "$outdir/tables/success_by_tol.tex", write replace


file write myfile " & Strictest &  & Preferred & & Least strict \\ "
file write myfile " & (1) & (2) & (3) & (4) & (5) \\ \hline "


// Panel A
file write myfile " \textbf{Panel A: Tax-liability approach} \\ "

local str1 "Baseline"
local str4 "With known deps."

foreach s in 1 4 {
	file write myfile "`str`s''"
	forval i = 1/5 {
		qui sum lb`i'_`s' [aw=soi_wgt]
		local entry : di %6.3f r(mean)
	
		if `i' == 3 & `s' == 1 {
			file write myfile " & \textbf{`entry'} "
		}
		else {
			file write myfile " & `entry' "
		}
	}
	if `s' == 1 {
		file write myfile " \\ \addlinespace[4pt] "
	}
	if `s' == 4 {
		file write myfile " \\ \quad \quad \& filing status \\ "
	}
}
file write myfile " \hline "

file write myfile " Tax difference tolerance " 
foreach k in `tax_thrlist' {
	file write myfile " & \\$`k' "
}


file write myfile " \\ \hline \addlinespace[10pt] "

*************************
*	Panel B		*
*************************

// Panel B
file write myfile " \textbf{Panel B: Item-based approach} \\ "

foreach s in 1 4 {
	file write myfile "`str`s''"
	forval i = 1/5 {
		qui sum ub`i'_`s' [aw=soi_wgt]
		local entry : di %6.3f r(mean)
	
		if `i' == 3 & `s' == 1 {
			file write myfile " & \textbf{`entry'} "
		}
		else {
			file write myfile " & `entry' "
		}
	}
	if `s' == 1 {
		file write myfile " \\ \addlinespace[4pt]  "
	}
	if `s' == 4 {
		file write myfile " \\ \quad \quad \& filing status \\ "
	}
}

file write myfile " \hline "

file write myfile " Credit tolerance " 
foreach k in `cred_thrlist' {
	file write myfile " & \\$`k' "
}
file write myfile " \\ "

file write myfile " Income/deduction tolerance " 
foreach k in `thrlist' {
	file write myfile " & \\$`k' "
}
file write myfile " \\ "





cap file close myfile















*************************************************
*												*
*			Make the graphs by income			*
*												*
*************************************************


gcollapse lb* ub* bad_* y = `incvar' [aw=soi_wgt], by(agi_bin) fast

	
qui replace y = y/1000
qui keep if y > 0
niceloglabels y, style(125) local(labstr)

*****************************
*	Upper and lower bound at preferred threshold	*
*****************************
twoway connected ub3_1 y || ///
	connected lb3_1 y, lpattern("__") msymbol(circle_hollow) ///
	xtitle("Income (000s)") ytitle("Share successful") ///
	scheme(s1color) ///
	legend(order(1 "Item-based approach" 2 "Tax-liability approach") cols(1)) ///
	xscale(log) xlabel(`labstr')
graph export "$outdir/figures/ub_lb_by_incm.eps", replace
graph export "$outdir/figures/ub_lb_by_incm.pdf", replace

list ub3_1 lb3_1 y



*********************
*	Upper, detail	*
*********************


twoway connected bad_sched_c y, mcolor(black) lcolor(black) msymbol(circle) msize(small) || ///
	connected bad_deps y, mcolor(black) lcolor(black) msymbol(circle_hollow) lpattern("__") msize(small) || ///
	connected bad_wages y, mcolor(black) lcolor(black) msymbol(triangle) lpattern("--") msize(small) || ///
	connected bad_itemizer y, mcolor(gray) lcolor(gray) msymbol(square) msize(small) || ///
	connected bad_rents_royal y, mcolor(gray) lcolor(gray) msymbol(pipe)  lpattern("__") || ///
	connected bad_txbl_pens y, mcolor(gray) lcolor(gray) msymbol(X) lpattern("--") ///
		xtitle("Income (000s)") ytitle("Share with failure situation") ///
		scheme(s1color) ///
		legend(order(1 "Sch C" 2 "Dpndnt" 3 "Wages mismatch"  4 "Itemizer" 5 "Rents/royalties" 6 "Pension/IRA") ///
			rows(2)) ///
		xscale(log) xlabel(`labstr')
		
		
		
graph export "$outdir/figures/bad_by_incm_broken.eps", replace
graph export "$outdir/figures/bad_by_incm_broken.pdf", replace



cap log close

